<?php

/**
 * @author dungdhs@gmail.com
 * @copyright 2012
 */

function theme_body_class($class=null){
  static $aClass = array();
  
  if (is_null($class)) return implode(' ', $aClass);
  
  $aClass[] = $class;
}

function theme_title($title=null){
	global $aOG;
  static $page_title=null;
  
  if (is_null($page_title)) $page_title = $aOG['og:title'];
  
  if (is_null($title)) return $page_title;
  
  $page_title = $title;
  __ogp_set('og:title', $title);
}

function theme_js($js_path=null){
  static $aJs = array();
  if (is_null($js_path)){
    $jsScript = '';
    $iScript = array();
    foreach ($aJs as $js){
      if (strpos($js, 'http')===0){
        $jsScript .= sprintf('<script language="javascript" type="text/javascript" src="%s"></script>', $js);
      }elseif (file_exists(PG_ROOT.$js)){
      	$jsScript .= sprintf('<script language="javascript" type="text/javascript" src="%s?v='.JS_VER.'"></script>', l($js));
      }else
        $iScript[] = $js;
    }
    
    $jsScript .= sprintf('<script language="javascript" type="text/javascript">$(document).ready(function (){%s});</script>', implode("\r\n", $iScript));
    return $jsScript;
  }

  $aJs[md5($js_path)] = $js_path;
}

function theme_css($css_path=null){
  static $aCss = array();
  
  if (is_null($css_path)){
    $cssScript = '';
    
    foreach ($aCss as $css){
      $cssScript .= sprintf('<link rel="stylesheet" href="%s?v='.JS_VER.'" title="stylesheet" type="text/css" />', l($css));
    }
    return $cssScript;
  }
  $aCss[md5($css_path)] = $css_path;
}

function truncate($str, $len){
  return strlen($str)>$len?substr($str, 0, $len-3).'...':$str;
}

function theme_pager($total_rows, $rows_per_page, $page_num){
  global $smarty;

  $pager = array();
	$last_page = ceil($total_rows / $rows_per_page);
  
	$page_num = (int) $page_num;
	if ($page_num < 1){
	  $page_num = 1;
	} 
	elseif ($page_num > $last_page){
	  $page_num = $last_page;
	}
  
	$upto = ($page_num - 1) * $rows_per_page;
	$pager['limit'] = 'LIMIT '.$upto.',' .$rows_per_page;
  
  $pager['current'] = $page_num;
	$pager['previous'] = ($page_num == 1)?theme_pager_link($page_num):theme_pager_link($page_num-1);
  $pager['next'] = ($page_num == $last_page)?theme_pager_link($page_num):theme_pager_link($page_num+1);
	$pager['last'] = $last_page;  
  $pager['lastlink'] = theme_pager_link($last_page);
  $pager['firstlink'] = theme_pager_link(1);
	$pager['info'] = 'Trang '.$page_num.'/'.$last_page;
	$pager['pages'] = theme_pager_surrounding_pages($page_num, $last_page, $pager['next']);
  
  $smarty->assign('pager', $pager);
  return $smarty->fetch('pager.tpl');
}

function theme_pager_surrounding_pages($page_num, $last_page, $next){
  global $crURL;
  
	$arr = array();
	$show = 5; // how many boxes
  
	// at first
	if ($page_num == 1){
		// case of 1 page only
		if ($next == $page_num) return array(1=>theme_pager_link(1));
		for ($i = 0; $i < $show; $i++){
			if ($i == $last_page) break;
			$arr[$i+1] = theme_pager_link($i+1);
		}
		return $arr;
	}
	// at last
	if ($page_num == $last_page){
		$start = $last_page - $show;
		if ($start < 1) $start = 0;
		for ($i = $start; $i < $last_page; $i++){
			$arr[$i+1] = theme_pager_link($i+1);
		}
		return $arr;
	}
	// at middle
	$start = $page_num - $show;
	if ($start < 1) $start = 0;
	for ($i = $start; $i < $page_num; $i++){
		$arr[$i+1] = theme_pager_link($i+1);
	}
  
	for ($i = $page_num; $i < ($page_num + $show); $i++){
		if ($i >= $last_page ) break;
		$arr[$i+1] = theme_pager_link($i+1);
	}
	return $arr;
}

function theme_pager_link($pageId){
  global $crURL;
  
  $aQuery = $_GET; unset($aQuery['q']);
  $aQuery['page'] = $pageId;
  return l($crURL, false, $aQuery);
}

function excel_export($data, $filename){
  include_once('include/class_phpexcel.php');
  
  // generate file (constructor parameters are optional)
  $xls = new Excel_XML('UTF-8', false, 'Sheet 1');
  $xls->addArray($data);
  $xls->generateXML($filename);
}

function theme_breadcrumb($in=null){
	static $aBreadcrumb=array();
	
	if (is_null($in)){
		$aOut = array(
			li('Trang chủ', '<front>')
		);
		
		if (count($aBreadcrumb)>0){
			foreach ($aBreadcrumb as $bc){
				if (isset($bc['link'])) $aOut[] = li($bc['title'], $bc['link']);
				else $aOut[] = $bc['title'];
			}
		}
		
		$aOut[] = theme_title();
		
		return implode(' > ', $aOut);
	}
	
	$aBreadcrumb = $in;
}
?>